# -*- coding:utf-8 -*-

import os
from openpyxl import load_workbook

class Excel():
    def __init__(self, excelpath, sheetname):
        book = load_workbook(excelpath)

        try:
            self.work_sheet = book.get_sheet_by_name(sheetname)
        except Exception as e:
            print e

    # 将数据以列表形式返回    -- 每一行一条用例
    # 使用用例编号读取测试用例
    def get_OneTestCase_row_RList(self,testNO=None):
        work_sheet = self.work_sheet

        # 循环生成列表，每条用例一个列表，以大的列表形式进行输出  [[a,b],[c,d]]
        datalist = []
        for r in range(2, work_sheet.max_row + 1):
            datalist1 = []
            for c in range(1, work_sheet.max_column + 1):
                val = work_sheet.cell(row=r, column=c).value
                datalist1.append(val)
            datalist.append(datalist1)

        for test in datalist:
            if testNO == test[0]:
                return test
        return datalist

    # 将数据以字典形式返回    -- 每一行一条用例
    def get_OneTestCase_row_RDist(self):
        work_sheet = self.work_sheet

        # 取表头，以列表的形式进行输出
        headlist = []
        for c in range(1, work_sheet.max_column + 1):
            val = work_sheet.cell(row=1, column=c).value
            headlist.append(val)

        datalist = self.get_OneTestCase_row_RList()

        # 循环生成字典，一条用例一个字典，在以大的列表进行输出     [{a:1,b:2}，{c:1,d:2}]
        alldatalist = []
        for m in range(len(datalist)):
            datadist = {}
            for n in range(len(headlist)):
                datadist[headlist[n]] = datalist[m][n]  # 单个形成字典
            alldatalist.append(datadist)
        # print alldatalist
        return alldatalist

    # 将数据以列表形式返回    -- 每一列一条用例
    def get_OneTestCase_column_RList(self):
        pass

    # 将数据以字典形式返回    -- 每一列一条用例
    def get_OneTestCase_column_RDist(self):
        work_sheet = self.work_sheet

        listRES = []
        for col in range(2, work_sheet.max_column + 1):
            dist = {}
            for row in range(1, work_sheet.max_row + 1):
                dist[work_sheet.cell(row=row, column=1).value] = work_sheet.cell(row=row, column=col).value
            listRES.append(dist)
        return listRES

    #FilePath单独进行读取，将数据以字典形式返回
    def Globe_dict(self):
        work_sheet = self.work_sheet

        redict = {}
        for row in range(2,work_sheet.max_row + 1):
            redict[work_sheet.cell(row=row,column=2).value] = work_sheet.cell(row=row,column=3).value
        return redict

if __name__ == '__main__':
    aa = Excel(os.getcwd() + u'\\inputdata\\TestCase.xlsx',u'PreProcessor')
    print aa.get_OneTestCase_row_RList("TP2")
